package com.woniu.controller;

import com.woniu.entity.Admins;
import com.woniu.service.RbacManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author WenXin
 * @version 1.0
 * @description: TODO
 * @date 2022/8/24 14:22
 */
@Controller//注解注册进Spring容器。添加访问映射url地址
@RequestMapping("/rbacManager")
public class RbacManagerController {
    //自动注入属性，实现类已经@Service注册进Spring容器了，这里会直接注入实现类
    @Autowired
    private RbacManagerService service;

    //可以单独给方法添加访问映射地址
    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Admins> all = service.findAll();
        model.addAttribute("data", all);
        return "success";
    }

    @RequestMapping("/findByName")//如果前端的参数跟后端不一样且无法修改，可以用注解@RequestParam取别名
    public String findByName(@RequestParam("name") String username, Model model) {
        List<Admins> list = service.findByName(username);
        model.addAttribute("data", list);
        return "success";
    }

    @RequestMapping(value = "/findByIds", method = RequestMethod.POST)
    public String findByIds(Integer[] ids, Model model) {
        List<Admins> list = service.findById(ids);
        model.addAttribute("data", list);
        return "success";
    }

    //List集合类型的参数
    @RequestMapping(value = "/findByLists", method = RequestMethod.POST)
    public String findByIds(@RequestParam List<Integer> ids, Model model) {
        Integer[] objects = (Integer[]) ids.toArray(new Integer[0]);
        List<Admins> list = service.findById(objects);
        model.addAttribute("data", list);
        return "success";
    }

    //Map集合类型的参数
    @RequestMapping("/addOneForMap")
    public String addOneForMap(@RequestParam Map<String, Object> userInfo, Model model) {
        if (userInfo != null) {
            int i = service.addOneByMap(userInfo);
            model.addAttribute("data", "增加的用户信息为：" + userInfo.get("username"));
        }
        return "success";
    }

    //对象类型的参数
    @RequestMapping("/addOne")
    public String addOne(Admins admins, Model model) {
        //需要保证前端传参的参数名，要跟实体类中的属性名匹配，不然无法赋值成功！
        int insert = service.insert(admins.getUsername(), admins.getPassword());
        model.addAttribute("data", "增加的用户为：" + admins);
        return "success";
    }

    //对象参数，删除操作delete
    @RequestMapping("/deleteOne")
    public String deleteOne(Admins admins, Model model) {
        //需要保证前端传参的参数名，要跟实体类中的属性名匹配，不然无法赋值成功！
        service.deleteByUserName(admins.getUsername());
        model.addAttribute("data", "删除的用户为：" + admins.getUsername());
        return "success";
    }

    //对象参数，更新操作update
    @RequestMapping("/updateOne")
    public String updateOne(Admins admins, Model model) {
        int update = service.update(admins);
        model.addAttribute("data", "修改用户的信息为：" + admins.getUsername());
        return "success";
    }

    //对象参数，查询对应用户
    @RequestMapping("/findByUserName")
    public String findByUserName(Admins admins, Model model) {
        List<Admins> byName = service.findByName(admins.getUsername());
        model.addAttribute("data", byName);
        return "success";
    }
}
